package org.dromara.starter.jpacomment.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.starter.jpacomment.enums.IndexType;
import org.dromara.starter.jpacomment.enums.Nullable;
import org.dromara.starter.jpacomment.mapper.JpacommentMapper;
import org.dromara.starter.jpacomment.properties.JpacommentProperties;
import org.dromara.starter.jpacomment.service.AlterCommentService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;

/**
 * 修改表注释和字段注释
 *
 * @author <a href="mailto:guzhongtao@middol.com">guzhongtao</a>
 */
public class MysqlAlterCommentServiceImpl  implements AlterCommentService {

    public static Logger log = LoggerFactory.getLogger(MysqlAlterCommentServiceImpl.class);
    private String schema;
    private JdbcTemplate jdbcTemplate;

    private JpacommentProperties jpacommentProperties;


    @Override
    public void setSchema(String schema) {

        this.schema = schema;
    }

    @Override
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 1数据库名称 2表名称 3注释
     */
    String updateTableComment = " ALTER TABLE %s.%s COMMENT = ?";


    /**
     * 1数据库名称 2表名称 3字段名称 4字段类型 5是否可以为空 6默认值 7字段注释
     */
    String sqlAlterUpdateColumn = "ALTER TABLE %s.%s MODIFY COLUMN %s %s %s DEFAULT %s COMMENT ?";

    /**
     * 1数据库名称 2表名称 3索引类型 4索引名称 5字段名称
     */
    String sqlAlterAddIndex = "ALTER TABLE %s.%s ADD %s INDEX %s(%s)";

    /**
     * 1数据库名称 2表名称 3索引类型 4索引名称 5字段名称
     */
    String sqlAlterDropIndex = "ALTER TABLE %s.%s DROP INDEX %s ";

    String sqlTableAllIndex = "SELECT index_name FROM information_schema.statistics "
        + "WHERE table_schema = ? AND table_name = ? AND index_name!='PRIMARY'"
        + "GROUP BY index_name";
    @Override
    public void alterTableComment(String tableName, String tableComment) {
        jdbcTemplate.update(String.format(updateTableComment, "`" + schema + "`", "`" + tableName + "`"), tableComment);
    }

    @Override
    public void alterColumn(String tableName, String columnName, String columnType, Nullable nullable , String defaultValue , String columnComment) {
//      baseMapper.alterUpdateColumn(schema,tableName, columnName, columnType,nullable.getValue(),defaultValue,columnComment);
        jdbcTemplate.update(String.format(sqlAlterUpdateColumn, "`" + schema + "`", "`" + tableName + "`", "`" + columnName + "`", columnType, nullable.getValue(), defaultValue, columnComment), columnComment);
    }


    @Override
    public void alterAddColumnIndex(String tableName, IndexType indexType, String indexName, List<String> indexCompositeNames) {
        String indexTypeString = indexType.getValue();
        StringJoiner columns = new StringJoiner(",");
        indexCompositeNames.forEach(columns::add);
        jdbcTemplate.update(String.format(sqlAlterAddIndex, "`" + schema + "`", "`" + tableName + "`", indexTypeString, indexName, columns));
    }

    @Override
    public void alterDropColumnIndex(String tableName) {
        List<String> indexNamesList = jdbcTemplate.query(sqlTableAllIndex, new Object[]{schema, tableName},
            new RowMapper<String>() {
                @Override
                public String mapRow(ResultSet rs, int rowNum) throws SQLException {
                    return rs.getString("index_name");
                }
            });
        indexNamesList.forEach(indexName->
            jdbcTemplate.update(String.format(sqlAlterDropIndex, "`" + schema + "`", "`" + tableName + "`", indexName))
        );
    }


    @Override
    public String getSchema() {
        return schema;
    }
}
